﻿/*
*    Qizx/open 3.0 beta
*
* This code is the open-source version of Qizx.
* Copyright (C) 2004-2009 Axyana Software -- All rights reserved.
*
* The contents of this file are subject to the Mozilla Public License 
*  Version 1.1 (the "License"); you may not use this file except in 
*  compliance with the License. You may obtain a copy of the License at
*  http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
*  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
*  for the specific language governing rights and limitations under the
*  License.
*
* The Initial Developer of the Original Code is Xavier Franc - Axyana Software.
*
*/
using System;
using System.Qizx.Api;
using DataModelException = System.Qizx.Api.DataModelException;
using EvaluationException = System.Qizx.Api.EvaluationException;
using Node = System.Qizx.Api.Node;
using QName = System.Qizx.Api.QName;
using QizxException = System.Qizx.Api.QizxException;
using XMLPullStream = System.Qizx.Api.XMLPullStream;
using PushStreamToDOM = System.Qizx.Api.Util.PushStreamToDOM;
using XQDateTimeBase = System.Qizx.Api.Util.XQDateTimeBase;
using DateTimeException = System.Qizx.Api.Util.XQDateTimeException;
using Duration = System.Qizx.Api.Util.XQDuration;
using NamespaceContext = System.Qizx.Util.NamespaceContext;
using Comparison = System.Qizx.Util.Basic.Comparison;
using FileUtil = System.Qizx.Util.Basic.FileUtil;
using ComparisonContext = System.Qizx.XQuery.ComparisonContext;
using XQItem = System.Qizx.XQuery.XQItem;
using XQValue = System.Qizx.XQuery.XQValue;
using StringValue = System.Qizx.XQuery.Dt.StringValue;
using UntypedAtomicType = System.Qizx.XQuery.Dt.UntypedAtomicType;
using URI = System.Uri;

namespace System.Qizx.Xdm {
    [Serializable]
    public class XdmText : XdmNode {
        internal XdmText(string text, XdmDocument doc)
            : this(doc.DataModel, text) {
        }
        public override string NodeKind {
            get {
                return "text";
            }
        }
        public override string LocalName {
            get { return "#text"; }
        }
        public override string Name {
            get { return "#text"; }
        }
        public override string NamespaceURI {
            get { return string.Empty; }
        }
        public override XdmNodeType NodeType {
            get {
                return System.Qizx.Api.XdmNodeType.Text;
            }

        }
        public override string StringValue {
            get {
                return _strValue;
            }
        }

        public virtual string TextValue {
            get {
                return _strValue;
            }
            set {
                _strValue = value;
            }
        }

        /// <summary>
        /// Gets the char value.
        /// </summary>
        /// <value>The char value.</value>
        public override char[] CharValue {
            get {
                return _strValue.ToCharArray();
            }
        }

        public override bool Atom {
            get {
                return NodeType >= System.Qizx.Api.XdmNodeType.Attribute;
            }

        }
        virtual public bool Whitespace {
            get {
                return false;
            }

        }

        internal string _strValue;

        internal XdmText(CoreDataModel model, string value)
            : base(model) {

            this._strValue = value;
        }

        internal XdmText(CoreDataModel model)
            : this(model, "") {
        }

        public override void AddText(string value) {
            if (string.IsNullOrEmpty(_strValue))
                this._strValue = value;
            else {
                _strValue += value;
            }
        }

        public override NodeSequenceBase GetChildren() {
            return System.Qizx.Xdm.SSequence.emptySequence;
        }

        public override Node[] Attributes {
            get { return null; }
        }

        public override string GetPrefixOfNamespace(string nsuri) {
            return _parent == null ? null : _parent.GetPrefixOfNamespace(nsuri);
        }

        public override string GetNamespaceOfPrefix(string prefix) {
            return _parent == null ? null : _parent.GetNamespaceOfPrefix(prefix);
        }

        public override bool IsElement {
            get { return false; }
        }

        public override string OuterXml {
            get { return this.StringValue; }
        }
        public override string InnerText {
            get {
                return this.StringValue;
            }
            set {
                this._strValue = value;
            }
        }
        public override string InnerXml {
            get {
                return this._strValue;
            }
            set {
                this._strValue = value;
            }
        }
        public override bool HasChildNodes {
            get { return false; }
        }

        public override void WriteTo(System.Xml.XmlWriter w) {
            w.WriteString(_strValue);
        }

        public override void WriteContentTo(System.Xml.XmlWriter w) {
            w.WriteString(_strValue);
        }

        public override string Prefix {
            get {
                return string.Empty;
            }
            set {

            }
        }
    }
}
